import 'package:flutter/material.dart';
import 'package:flutter_bmfmap_example/service/deviceservice.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class DeviceInfomodel with ChangeNotifier {
  List<Deviceitemmodel> devicelist = [];

  Future<void> init() async {
    var result = await Deviceservice.getDeviceLisst();
    devicelist = DeviceListmodel.fromJson(result).list;
    notifyListeners();
  }

  int getnumbyType(int type) {
    int num = 0;
    for (int i = 0; i < devicelist.length; i++) {
      if (devicelist[i].typeId == type) {
        num++;
      }
    }
    return num;
  }

  List<Deviceitemmodel> getlistbyType(int type) {
    List<Deviceitemmodel> list = [];
    for (int i = 0; i < devicelist.length; i++) {
      if (devicelist[i].typeId == type) {
        list.add(devicelist[i]);
      }
    }
    return list;
  }

  Future<void> operatedevice(int type, int id, String operation) async {
    EasyLoading.show(status: '请稍等');
    int result = await Deviceservice.operatedevice(type, id, operation);
    print(result);
    EasyLoading.dismiss();
  }

  
}

class DeviceListmodel {
  final List<Deviceitemmodel> list;

  DeviceListmodel(this.list);

  factory DeviceListmodel.fromJson(List<dynamic> itemlist) {
    return DeviceListmodel(
      itemlist.map((item) => Deviceitemmodel.fromJson(item)).toList(),
    );
  }
}

class Deviceitemmodel {
  int deviceId;
  int typeId;
  int roomId;
  String name;
  int state;
  String content;

  Deviceitemmodel({
    this.deviceId,
    this.typeId,
    this.roomId,
    this.name,
    this.state,
    this.content,
  });

  factory Deviceitemmodel.fromJson(dynamic item) {
    return Deviceitemmodel(
      deviceId: item['deviceId'],
      typeId: item['typeId'],
      roomId: item['roomId'],
      name: item['name'],
      state: item['state'],
      content: item['content'],
    );
  }
}
